<?php
namespace app\index\controller;

class Login  {
	public function index() {
        return view('/index/login');
    }

    public function login() {
        $username = input('post.username');
        $password = input('post.password');
        // 成功接受数据
        if ($username != null and $password != null) {
            $cuser = model('User');
            $user_data = $cuser::where('username', $username)->where('password', $password)->find();
            // 成功查询到用户
            if ($user_data != null) {
                session('user_id', $user_data->id);
                session('fullname', $user_data->fullname);
                session('introduction', $user_data->introduction);
                session('avator_path', $user_data->avator_path);
                session('admin', $user_data->admin);
                return json([
                    'status' => 'success',
                    'message' => '登录成功！',
                    'data' => '/index'
                ]);
            }
            // 未查询到用户
            else {
                return json([
                    'status' => 'error',
                    'message' => '用户名或密码输入错误！',
                    'data' => null
                ]);
            }            
        }
        // 未接收到数据
        else {
            return json([
                'status' => 'error',
                'message' => '提交错误，请重试！',
                'data' =>  null
            ]);
        }
        
    }

    public function logout() {
    	session(null);
    	return action('index/index');
    }
}